package com.monsterPatties.utils.vectorCache 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author jc
	 */
	public class VectorCache extends Sprite
	{
		
		/*------------------------------------------------------------------Constant----------------------------------------------------------------------------*/
		
		
		/*------------------------------------------------------------------Properties----------------------------------------------------------------------------*/
		public var cacheBmd:Array;
		private var _mcHolder:Sprite;
		private var _mc:MovieClip;
		public var currentFrame:int;
		
		
		/*------------------------------------------------------------------Constructor----------------------------------------------------------------------------*/
		
		
		public function VectorCache() 
		{
			
		}
		
		/*------------------------------------------------------------------Methods----------------------------------------------------------------------------*/
		
		
		
		public function extractFrames( mc:MovieClip ):void 
		{
			cacheBmd = new Array();
			var frameCount:int = mc.totalFrames;
			trace( "cnt", frameCount );	

			for( var i:int =1; i <= frameCount; i++	){
				mc.gotoAndStop( i );
				var bmd:BitmapData = new BitmapData( mc.width, mc.height, true , 0  );
				var bm:Bitmap = new Bitmap( bmd );
				var matrix:Matrix = new Matrix();
				var rect:Rectangle = mc.getBounds( mc );
				matrix.translate( -rect.x , -rect.y );
				bmd.draw( mc ,matrix );
				cacheBmd.push( bm );
			}
			
			currentFrame = 0;
			_mcHolder = new Sprite();
			_mcHolder.addChild( cacheBmd[ currentFrame ] );
			addChild( _mcHolder );			
		}
		
		public function playCache():void 
		{
			addEventListener( Event.ENTER_FRAME, onPlayMc );
		}
		
		public function gotoAndStopCache( index:int ):void {
			_mcHolder.removeChildAt( 0 );
			if( index >= cacheBmd.length ){
				index = cacheBmd.length - 1;
			}			
			_mcHolder.addChildAt( cacheBmd[ index ], 0 );
		}
		
		
		/*------------------------------------------------------------------Getters----------------------------------------------------------------------------*/
		
		/*------------------------------------------------------------------Setters----------------------------------------------------------------------------*/
		
		/*------------------------------------------------------------------EventHandlers----------------------------------------------------------------------------*/
		
		private function onPlayMc ( e:Event ):void{	
			var mcCnt:int = _mcHolder.numChildren -1;	
			while( mcCnt >= 0 ){
				var child:* = _mcHolder.getChildAt( mcCnt );
				
				if( _mcHolder.contains( child ) ){
					_mcHolder.removeChild( child );
					currentFrame++;
					if( currentFrame > cacheBmd.length - 1 ){
						currentFrame=0;
					}
					_mcHolder.addChildAt( cacheBmd[ currentFrame ] , mcCnt );
				}
				
				mcCnt--;
			}
		}		
	}

}